import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * User: 12629
 * Date: 2022/2/17
 * Time: 18:29
 * Description:
 */


public class TestDemo {

    public static void swap(int a,int b) {
        int tmp = a;
        a = b;
        b = tmp;
        //System.out.println("交换后："+a+" "+b);
    }

    public static void main(String[] args) {
        int x = 10;
        int y = 20;
        System.out.println("交换前："+x+" "+y);
        swap(x,y);
        System.out.println("交换后："+x+" "+y);
    }

    public static int facSum1(int n) {
        int sum = 0;
        for (int i = 1; i <= n; i++) {
            int ret = 1;
            for (int j = 1; j <= i; j++) {
                ret *= j;
            }
            sum += ret;
        }
        return sum;
    }

    public static int facSum(int n) {
        System.out.println("facSum方法被执行了！");
        int sum = 0;
        for (int i = 1; i <= n; i++) {
            sum += fac(i);
        }
        return sum;
    }

    /**
     * 求数字n的阶乘
     * @param n
     * @return
     */
    public static int fac(int n) {
        int ret = 1;
        for (int j = 1; j <= n; j++) {
            ret *= j;
        }
        return ret;
    }

    public static void main13(String[] args) {
//        int ret = facSum(5);
//        System.out.println(ret);
        System.out.println(facSum(5));
        System.out.println(facSum(3));
        System.out.println(facSum(9));
        System.out.println(facSum(7));
    }


    /**
     * 判断某一年是不是闰年的方法
     * @param year 年份
     * @return true代表是  false代表不是闰年
     */
    public static boolean isLeapYear(int year) {
        if(year%4==0 && year%100!=0  || year % 400 == 0) {
            return true;
        }
        return false;
    }

    //按值传递
    public static int add(int a ,int b) {
        return a+b;
        //System.out.println(a+b);
    }

    public static void func() {
        int x = 10;
        System.out.println("fdsafsa");
//        public static void func2() {
//
//        }
    }

    public static void main12(String[] args) {
        //实参
        int a = 1;
        int b = 2;
        int ret = add(a,b);
        System.out.println(ret);

    }

    public static void main11(String[] args) {
        int n = 7;
        for (int i = 31; i >= 1; i-=2) {
            System.out.print((n>>>i)&1);
        }
        System.out.println();
        for (int i = 30; i >= 0; i-=2) {
            System.out.print((n>>>i)&1);
        }
    }

    public static void main10(String[] args) {
        int n = -1;
        int count = 0;
        while (n != 0) {
            count++;
            n = n & (n-1);
        }
        System.out.println(count);
    }

    /**
     * 优点是  随时判断 当前是否还需要继续右移
     * @param args
     */
    public static void main9(String[] args) {
        int n = 10;
        int count = 0;
        while (n != 0) {
            if((n&1) != 0) {
                count++;
            }
            n = n>>>1;
        }
        System.out.println(count);
    }

    public static void main8(String[] args) {
        int n = -1;
        int count = 0;
        for (int i = 0; i < 32; i++) {
            if(((n>>>i)&1) != 0) {
                count++;
            }
        }
        System.out.println(count);
    }


    public static void main7(String[] args) {

        for (int i = 0; i < 999999; i++) {
            int tmp = i;
            int count = 0;//计算当前的数字i有多少位？
            while (tmp != 0) {
                count++;//3
                tmp = tmp / 10;
            }
            //tmp成为0了
            tmp = i;//求 tmp的每一位是几？123
            int sum = 0;
            while (tmp != 0) {
                sum += Math.pow(tmp%10,count);
                tmp /= 10;//123/10=12
            }
            if(sum == i) {
                System.out.println(i);
            }
        }
    }

    public static void main6(String[] args) {
        int a = 18;
        int b = 24;
        int c = a%b;//18
        while (c!=0) {
            a = b;//24
            b = c;//18
            c = a % b;
        }
        System.out.println(b);
    }

    public static void main5(String[] args) {
        for (int i = 1; i <= 9 ; i++) {
            for (int j = 1 ; j <= i ; j++) {
                System.out.print(i+"*"+j +"="+i*j+" ");
            }
            System.out.println();
        }
    }

    public static void main4(String[] args) {
        //int n = 7;
        for (int n = 1; n < 101; n++) {
            int i = 2;
            for (; i <= Math.sqrt(n); i++) {
                if(n % i == 0) {
                    //System.out.println("n不是素数！");
                    break;
                }
            }
            if(i > Math.sqrt(n)) {
                System.out.println(n+"是素数！");
            }
        }
    }


    public static void main3(String[] args) {
        int n = 7;
        int i = 2;
        for (; i <= n/2; i++) {
            if(n % i == 0) {
                System.out.println("n不是素数！");
                break;
            }
        }
        if(i > n/2) {
            System.out.println(n+"是素数！");
        }
    }

    public static void main2(String[] args) {
        int n = 7;
        int i = 2;
        for (; i < n; i++) {
            if(n % i == 0) {
                System.out.println("n不是素数！");
                break;
            }
        }
        //当代码走到第20行的时候
        if(i == n) {
            System.out.println(n+"是素数！");
        }
    }

    public static void main1(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int count = 3;
        while (count != 0) {
            System.out.println("请输入密码：");
            String password = scanner.nextLine();
            //if(password == "123456") { 注意：Java当中比较字符串是否相同 使用equals方法
            if(password.equals("123456")) {
                System.out.println("登录成功！");
                break;
            }else {
                count--;
                System.out.println("密码错误！你还有"+count+"次机会！");
            }
        }
    }
}
